<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\middleware\api\Auth;
use app\validate\api\CheckId;


class Collect extends BaseController
{

    protected $middleware=[Auth::class];

    /**
     * 收藏商品or取消
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        (new CheckId())->goCheck();
        $products_id=request()->param('id');
        $users_id=request()->userid;
        $model= \app\model\Collect::where('users_id',$users_id)->where('products_id',$products_id)->find();

        if(!$model){
            $model=new \app\model\Collect();
            $model->users_id=$users_id;
            $model->products_id=$products_id;
            $model->save();
        }else{
            $model->delete();
        }
        return $this->success();
    }


    /**
     * 我的收藏
     * @param int $limit
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function myCollect($limit=10)
    {
        $data=\app\model\Collect::where('users_id',request()->userid)
            ->with('product')
            ->paginate($limit,false);

        return $this->success($data);
    }


}
